package com.hsmw.api.controller.biz;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hsmw.api.mapper.*;
import com.hsmw.api.service.*;
import com.hsmw.common.base.BaseController;
import com.hsmw.common.base.BasePage;
import com.htn.common.core.base.DataResponse;
import com.hsmw.domain.HsmwMenu;
import com.hsmw.domain.form.GetObjectByIdForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

import static com.htn.common.core.constant.CommonConstants.ResponseStatus.SUCCESS;

/**
 * 菜单管理
 *
 * @author WD
 */
@Api(tags = "BIZ-菜单管理")
@RestController
@RequestMapping("/api/biz/menu")
public class BizMenuController extends BaseController<HsmwMenuService, HsmwMenu> {

	@Autowired
	private HsmwMenuService hsmwMenuService;


	@Autowired
	private HsmwPmUserService hsmwPmUserService;

	@Autowired
	private HsmwConcreteUserService hsmwConcreteUserService;

	@Autowired
	private HsmwDriverUserService hsmwDriverUserService;

	@Autowired
	private HsmwBizUserService hsmwBizUserService;

	@Autowired
	private HsmwHtnUserService hsmwHtnUserService;

	@Autowired
	private HsmwTpspUserService hsmwTpspUserService;

	@Autowired
	private HsmwSaleUserService hsmwSaleUserService;


	@Autowired
	private HsmwPmUserMapper hsmwPmUserMapper;

	@Autowired
	private HsmwConcreteUserMapper hsmwConcreteUserMapper;

	@Autowired
	private HsmwDriverUserMapper hsmwDriverUserMapper;

	@Autowired
	private HsmwBizUserMapper hsmwBizUserMapper;

	@Autowired
	private HsmwHtnUserMapper hsmwHtnUserMapper;

	@Autowired
	private HsmwTpspUserMapper hsmwTpspUserMapper;

	@Autowired
	private HsmwSaleUserMapper hsmwSaleUserMapper;


	/**
	 * 分页
	 *
	 * @param basePage
	 * @return
	 */
	@ApiOperation(value = "获取菜单分页列表", notes = "获取菜单分页列表", produces = "application/json")
	@PostMapping("/getHsmwMenuPage")
	public DataResponse getHsmwMenuPage(@RequestBody @Valid BasePage<HsmwMenu> basePage) {
		IPage page = new Page(basePage.getPageNo(), basePage.getPageSize());
		HsmwMenu hsmwMenu = basePage.getRequestData();
		IPage<HsmwMenu> data = hsmwMenuService.getHsmwMenuPage(page, hsmwMenu);
		return new DataResponse(data);
	}

	/**
	 * 树状菜单
	 *
	 * @return
	 */
	@ApiOperation(value = "获取菜单列表", notes = "获取菜单列表", produces = "application/json")
	@PostMapping("/getHsmwMenuList")
	public DataResponse getHsmwMenuList(@RequestBody GetObjectByIdForm form) {
		HashMap<Object, Object> data = hsmwMenuService.getHsmwMenuList(form.getKey());
		return new DataResponse(data);
	}

	/**
	 * 根据ID查询数据
	 *
	 * @param form
	 * @return
	 */
	@ApiOperation(value = "根据id查询数据", notes = "根据id查询数据", produces = "application/json")
	@PostMapping("/getHsmwMenuById")
	public DataResponse getHsmwMenuById(@RequestBody GetObjectByIdForm form) {
		HsmwMenu hsmwMenu = hsmwMenuService.getById(form.getKey());
		return new DataResponse(SUCCESS, hsmwMenu);
	}

	/**
	 * 新增
	 * 修改  必须带ID
	 *
	 * @param hsmwMenu
	 * @return
	 */
	@ApiOperation(value = "新增/修改", notes = "新增/修改/", produces = "application/json")
	@PostMapping("/saveOrUpdate")
	public DataResponse saveOrUpdate(@RequestBody HsmwMenu hsmwMenu) {
		Assert.notNull(hsmwMenu.getPath(), "子路由使用的path不能为空");
		Assert.notNull(hsmwMenu.getName(), "权限名称不能为空");
		Assert.notNull(hsmwMenu.getMeta(), "前端用来控制路由的元数据不能为空");
		Assert.notNull(hsmwMenu.getType(), "该权限所属的终端类型不能为空");
		Assert.notNull(hsmwMenu.getComponent(), "匹配地址不能为空");
		return hsmwMenuService.saveOrUpdateHsmwMenu(hsmwMenu);
	}
}
